package cn.itcast.web.servlet;

import Utils.AliyunSmsUtils;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet("/SmsUtilsServlet")
public class SmsUtilsServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            String phone=request.getParameter("phone");

            AliyunSmsUtils aliyunSmsUtils=new AliyunSmsUtils();
            aliyunSmsUtils.setNewcode();
            String code = Integer.toString(aliyunSmsUtils.getNewcode());
            try {
                SendSmsResponse rep = AliyunSmsUtils.sendSms(phone, code); //
                if (rep.getCode().equals("OK")){
                    response.getWriter().append(code);
                }else {
                    response.getWriter().append("");
                }
                System.out.println("短信接口返回的数据----------------");
                System.out.println("Code=" + rep.getCode());
                System.out.println("Message=" + rep.getMessage());
                System.out.println("RequestId=" + rep.getRequestId());
                System.out.println("BizId=" + rep.getBizId());
            } catch (ClientException e) {
                throw new RuntimeException(e);
            }
    }
}
